package com.tarena.lbs.content.service.impl;


import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.common.content.enums.ContentResultEnum;
import com.tarena.lbs.content.mapper.ArticleCategoryMapper;
import com.tarena.lbs.content.repository.ContentRepository;
import com.tarena.lbs.content.service.ArticleCategoryService;
import com.tarena.lbs.pojo.content.dos.ArticleCategory;
import com.tarena.lbs.pojo.content.dto.ArticleCategoryDTO;
import com.tarena.lbs.pojo.content.param.ArticleCategoryParam;
import com.tarena.lbs.pojo.content.query.ArticleCategoryQuery;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ArticleCategoryServiceImpl implements ArticleCategoryService {

    @Resource
    private ArticleCategoryMapper articleCategoryMapper;

    @Override
    public void insert(ArticleCategoryParam articleCategoryParam) {

        //参数实体类映射
        ArticleCategory articleCategory = new ArticleCategory();
        BeanUtils.copyProperties(articleCategoryParam, articleCategory);

        //创建时间，更新时间设值
        Date date = new Date();
        articleCategory.setCreateTime(date);
        articleCategory.setUpdateTime(date);

        //插入数据库中
        articleCategoryMapper.insertSelective(articleCategory);
    }

    @Override
    public void delete(Integer id) throws BusinessException {
        //删除前校验，不存在的数据直接返回
        ArticleCategoryDTO articleCategoryDTO = get(id);
        if (articleCategoryDTO == null) {
            throw new BusinessException(ContentResultEnum.DATA_NOT_FIND);
        }
        //根据id删除数据库中数据
        articleCategoryMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(ArticleCategoryParam articleCategoryParam) throws BusinessException {
        Integer id = articleCategoryParam.getId();
        //从数据库根据id查询数据
        ArticleCategory res = articleCategoryMapper.selectByPrimaryKey(id);
        if (res == null) {
            throw new BusinessException(ContentResultEnum.DATA_NOT_FIND);
        }
        //更新数据，并且更新时间
        ArticleCategory articleCategory = new ArticleCategory();
        BeanUtils.copyProperties(articleCategoryParam, articleCategory);
        articleCategory.setUpdateTime(new Date());
        articleCategoryMapper.updateByPrimaryKeySelective(articleCategory);
    }


    @Override
    public PageResult<ArticleCategoryDTO> list(ArticleCategoryQuery articleCategoryQuery) {
        //分页参数
        int page = articleCategoryQuery.getPageNo();
        int pageSize = articleCategoryQuery.getPageSize();

        //查询数据库，动态参数条件拼接
        List<ArticleCategory> list = articleCategoryMapper.selectByCondition(articleCategoryQuery);

        PageInfo<ArticleCategory> pageInfo = new PageInfo<>(list);

        //返回值封装
        List<ArticleCategoryDTO> categoryDTOList = pageInfo.getList().stream().map(articleCategory -> {
            ArticleCategoryDTO articleCategoryDTO = new ArticleCategoryDTO();
            BeanUtils.copyProperties(articleCategory, articleCategoryDTO);
            return articleCategoryDTO;
        }).collect(Collectors.toList());

        PageResult<ArticleCategoryDTO> pageResult = new PageResult<>();
        pageResult.setObjects(categoryDTOList);
        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setPageNo(page);
        pageResult.setPageSize(pageSize);
        return pageResult;
    }

    @Override
    public ArticleCategoryDTO get(Integer id) throws BusinessException {
        //从数据库根据主键查询，不存在直接返回，存在的话，封装返回值
        ArticleCategory articleCategory = articleCategoryMapper.selectByPrimaryKey(id);
        if (articleCategory == null) {
            throw new BusinessException(ContentResultEnum.DATA_NOT_FIND);
        }
        ArticleCategoryDTO articleCategoryDTO = new ArticleCategoryDTO();
        BeanUtils.copyProperties(articleCategory, articleCategoryDTO);
        return articleCategoryDTO;
    }
}
